package com.dianrui.qiyouriding.activity;

import android.view.View;
import android.widget.TextView;

import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.event.RfreshAgreementList;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查看合同信息
 *
 * @author lzy
 */
@SuppressWarnings("ALL")
public class AgreementInfoActivity extends BaseActivity {
    private String contractIds;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.code)
    TextView code;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.number)
    TextView number;

    @Override
    public int getContentViewId() {
        return R.layout.activity_agreement_info;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.agreementinfo_txt));
        customInit(true, R.color.main_color);
        if (getIntent() != null && getIntent().getExtras() != null) {
            contractIds = getIntent().getStringExtra("contract_id");
        }
        getShowContract();
    }

    private void getShowContract() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("contract_id", contractIds);
        OkHttpRequest.getInstance().postWithMemberToken(Url.SHOWCONTRACT, jsonObject.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                code.setText(jsonObject.optJSONObject("data").optString("order_code"));
                                money.setText(jsonObject.optJSONObject("data").optString("money"));
                                time.setText(jsonObject.optJSONObject("data").optString("create_time"));
                                name.setText(jsonObject.optJSONObject("data").optString("name"));
                                number.setText(jsonObject.optJSONObject("data").optString("number"));
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void failedRequest(String result) {

            }
        });
    }

    @Override
    public void initData() {
    }


    @OnClick({R.id.back, R.id.cancel_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.cancel_agreement:
                cancelAgreement();
                break;

            default:
                break;
        }
    }

    //取消合同
    private void cancelAgreement() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("contract_id", contractIds);
        OkHttpRequest.getInstance().postWithMemberToken(Url.CANCLECONTRACT, jsonObject.toString(),  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject != null && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                EventBus.getDefault().post(new RfreshAgreementList());
                                ToastUtil.showToast(jsonObject.optString("message"));
                                finish();
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }
}
